google storage api | test create bucket | fetch file or stream | Search

This code defines a utility function called streamToOutput that downloads a file from a URL or processes a stream and saves it to a local file. It handles directory creation and utilizes imported functions for file fetching and stream processing.

Run example

npm run import -- "test stream to output"

test stream to output

var fs = require('fs');
var path = require('path');
var importer = require('../Core');
var mkdirpSync = importer.import("mkdirp");
var fetchOrStream = importer.import("fetch file or stream");

// locally based utility for editing styles
function streamToOutput(fileUrl, bucketName, stream) {
    var outputPath = path.join(path.resolve(process.env.PROJECT_OUTPUT),
                               fileUrl.replace(/\?.*/ig, ''));
    mkdirpSync(path.dirname(outputPath));
    var writeStream = fs.createWriteStream(outputPath);
    return fetchOrStream(stream || fileUrl, writeStream)
        .then(() => outputPath)
}

module.exports = streamToOutput;

What the code could have been:

// Import required modules
const fs = require('fs');
const path = require('path');
const { importCoreFunctions } = require('../Core');

// Define utility function to stream a file or URL to output
/**
 * Streams a file or URL to the specified output path.
 * 
 * @param {string} fileUrl The URL or path to the file to be streamed.
 * @param {string} bucketName The name of the bucket (currently unused).
 * @param {ReadableStream} stream The ReadableStream containing the file data (or null for file URL).
 * @returns {Promise} The path to the streamed file.
 */
function streamToOutput(fileUrl, bucketName, stream = null) {
    // Construct the output path
    const outputPath = path.join(
        path.resolve(process.env.PROJECT_OUTPUT),
        fileUrl.replace(/\?.*/ig, '')
    );

    // Create the output directory if it doesn't exist
    importCoreFunctions.mkdirpSync(path.dirname(outputPath));

    // Create a write stream for the output file
    const writeStream = fs.createWriteStream(outputPath);

    // Stream the file or URL to the output file
    return importCoreFunctions.fetchOrStream(stream, writeStream)
       .then((outputPath) => outputPath);
}

// Export the utility function
module.exports = streamToOutput;

This code defines a utility function called streamToOutput that takes a file URL, a bucket name, and an optional stream as input.

Here's a breakdown:

  1. Dependencies:

  2. Imported Functions:

  3. streamToOutput Function:

  4. Export:

In essence, this code provides a reusable function for downloading files from URLs or processing streams and saving them to local files.